package at.mri.dogwatch.client.pages.newestfeed;

import at.mri.dogwatch.shared.proxy.VideoProxy;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safecss.shared.SafeStyles;
import com.google.gwt.safecss.shared.SafeStylesUtils;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.projectplace.gwt.reltime.client.RelativeTime;

public class VideoProxyCell extends AbstractCell<VideoProxy> {

	interface Templates extends SafeHtmlTemplates {

		@SafeHtmlTemplates.Template("<p style='{0}'>{1}</p>")
		SafeHtml text(SafeStyles styles, SafeHtml text);

		@SafeHtmlTemplates.Template("<p>{0}</p>")
		SafeHtml text(SafeHtml text);
		
		@SafeHtmlTemplates.Template("<p><small><i>{0}</i></small></p>")
		SafeHtml subText(SafeHtml text);
		
		@SafeHtmlTemplates.Template("<p><h3>{0}</h3></p>")
		SafeHtml title(SafeHtml text);

		@SafeHtmlTemplates.Template("<img src='{0}' width='120' height='90'/>")
		SafeHtml img(String url);

		@SafeHtmlTemplates.Template("http://i2.ytimg.com/vi/{0}/default.jpg")
		SafeHtml youtubeImgLink(SafeHtml youtubeId);

		//@formatter:off
		@SafeHtmlTemplates.Template("<table>\n" + 
									"  <tr>\n" + 
									"    <td rowspan='3'>{0}</td>\n" + 
									"    <td><div>" +
									"		{1}" +
									"		{2}" +
									"		{3} </div></td>\n" + 
									"  </tr>\n" + 
									//"  <tr>\n" + 
									//"    <td>{2}</td>\n" + 
									//"  </tr>\n" + 
									//"  <tr>\n" + 
									//"    <td>{3}</td>\n" + 
									//"  </tr>\n" + 
									"</table>")
		SafeHtml cell(SafeHtml img, SafeHtml title, SafeHtml author,
				SafeHtml viewCount);
		//@formatter:on
	}
	
	public VideoProxyCell() {
		
	}
	
	private static Templates templates = GWT.create(Templates.class);
	private static RelativeTime relativeTime = GWT.create(RelativeTime.class);

	@Override
	public void render(com.google.gwt.cell.client.Cell.Context context,
			VideoProxy value, SafeHtmlBuilder sb) {
		if (value != null) {
			SafeHtml safeTitle = templates.title(SafeHtmlUtils.fromString(value
					.getTitle()));

			SafeHtml safeAuthor = SafeHtmlUtils.fromSafeConstant("");
			if (value.getAuthor() != null) {
				safeAuthor = templates.text(SafeHtmlUtils.fromString(value
						.getAuthor().getUserName()));
			}

			SafeHtml image = templates.img(templates
					.youtubeImgLink(SafeHtmlUtils.fromSafeConstant(value
							.getYoutubeTag())).asString());

			SafeHtml viewsAndDate =templates.subText(SafeHtmlUtils.fromSafeConstant("10 views, " + relativeTime.format(value.getLastModifiedDate())));
			SafeHtml rendered = templates.cell(image, safeTitle, safeAuthor,
					viewsAndDate);
			sb.append(rendered);

		}

	}
}
